﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;
using GCC.Container.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.DeviceServer.ApiModels.Command;
using GCC.Container.LogProviders;
using System.Net;
 
using System.Collections.Concurrent;
using System.DeviceServer.Models;
using System.DeviceServer.MyUDP;

namespace System.DeviceServer
{

    [ApiController]
    [Route("[controller]")]
    public class DeviceControlController : GCCAPIControllerBase
    {
        public DeviceControlController(AbsGCCContext context) : base(context)
        {

        }

        [HttpGet("focusReduce/id/{id}")]
        public DataResponse<bool> SendFocusReduceCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendFocusReduceCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD4, 0x01, 0x00, 0x00, 0x00, 0xE6, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }
        [HttpGet("focusAdd/id/{id}")]
        public DataResponse<bool> SendFocusAddCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendFocusAddCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD4, 0x00, 0x00, 0x00, 0x00, 0xE5, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("roughtfocusReduce/id/{id}")]
        public DataResponse<bool> SendRoughtFocusReduceCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendRoughtFocusReduceCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD5, 0x01, 0x00, 0x00, 0x00, 0xE7, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }
        [HttpGet("roughtfocusAdd/id/{id}")]
        public DataResponse<bool> SendRoughtFocusAddCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendRoughtFocusAddCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD5, 0x00, 0x00, 0x00, 0x00, 0xE6, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("continousfocusReduce/id/{id}")]
        public DataResponse<bool> SendCFocusReduceCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCFocusReduceCommand", null);
            byte[] startCommand = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD3, 0x02, 0x00, 0x00, 0x00, 0xE6, 0xF8 };
            byte[] stopCommand = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD3, 0x00, 0x00, 0x00, 0x00, 0xE4, 0xF8 };
            sendResult = client.Send(startCommand);
            sendResult = client.Send(stopCommand);
            return HandleData(sendResult);
        }
        [HttpGet("continousfocusAdd/id/{id}")]
        public DataResponse<bool> SendCFocusAddCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCFocusAddCommand", null);
            byte[] startCommand = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD3, 0x01, 0x00, 0x00, 0x00, 0xE5, 0xF8 };
            byte[] stopCommand = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xD3, 0x00, 0x00, 0x00, 0x00, 0xE4, 0xF8 };
            sendResult = client.Send(startCommand);
            sendResult = client.Send(stopCommand);
            return HandleData(sendResult);
        }

        [HttpGet("autofocus/id/{id}")]
        public DataResponse<bool> SendAutoFocusCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendAutoFocusCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xDA, 0x00, 0x00, 0x00, 0x00, 0xEB, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("saveparams/id/{id}")]
        public DataResponse<bool> SendSaveParamsCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendSaveParamsCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xEE, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("shuttercorrect/id/{id}")]
        public DataResponse<bool> SendShutterCorrectCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendShutterCorrectCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x10, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("backgroundcorrect/id/{id}")]
        public DataResponse<bool> SendBackGroundCorrectCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendBackGroundCorrectCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x11, 0x00, 0x00, 0x00, 0x00, 0x22, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("bitselftest/id/{id}")]
        public DataResponse<bool> SendBitSelfTestCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendBitSelfTestCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x0D, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("blindpixelcorrect/id/{id}")]
        public DataResponse<bool> SendBlindPixelCorrectCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendBlindPixelCorrectCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x35, 0x00, 0x00, 0x00, 0x00, 0x46, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("saveblindpixelcorrect/id/{id}")]
        public DataResponse<bool> SendSaveBlindPixelCorrectCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendSaveBlindPixelCorrectCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x4B, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("crossup/id/{id}")]
        public DataResponse<bool> SendCrossUpCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCrossUpCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x32, 0x00, 0x00, 0x00, 0x00, 0x43, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("crossdown/id/{id}")]
        public DataResponse<bool> SendCrossDownCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCrossDownCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x32, 0x01, 0x00, 0x00, 0x00, 0x44, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("crossleft/id/{id}")]
        public DataResponse<bool> SendCrossLeftCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCrossLeftCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x32, 0x02, 0x00, 0x00, 0x00, 0x45, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("crossright/id/{id}")]
        public DataResponse<bool> SendCrossRightCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCrossRightCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x32, 0x03, 0x00, 0x00, 0x00, 0x46, 0xF8 };
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("crossitem/id/{id}")]
        public DataResponse<bool> SendCrossItemCommand([FromRoute] long id, [FromQuery] byte value)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCrossItemCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8 };
            command[6] = value;
            int sum = 0;
            for (int i = 2; i < command.Length - 1; i++)
            {
                sum += command[i];
            }
            command[10] = (byte)(sum % 256);
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("crossstep/id/{id}")]
        public DataResponse<bool> SendCrossStepCommand([FromRoute] long id, [FromQuery] byte value)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendCrossStepCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8 };
            command[6] = value;
            int sum = 0;
            for (int i = 2; i < command.Length - 1; i++)
            {
                sum += command[i];
            }
            command[10] = (byte)(sum % 256);
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("blindpixelthreshold/id/{id}")]
        public DataResponse<bool> SendBlindPixelThresholdCommand([FromRoute] long id, [FromQuery] ushort threshold)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendBlindPixelThresholdCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8 };
            command[6] = (byte)(threshold / 256);
            command[7] = (byte)(threshold - command[6]*256);
            int sum = 0;
            for (int i = 2; i < command.Length - 1; i++)
            {
                sum += command[i];
            }
            command[10] = (byte)(sum % 256);
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("movecross/id/{id}")]
        public DataResponse<bool> SendMoveCrossCommand([FromRoute] long id, [FromQuery] ushort xpos, [FromQuery] ushort ypos)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("MoveCrossCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8 };
            command[6] = (byte)(xpos / 256);
            command[7] = (byte)(xpos - command[6]*256);
            command[8] = (byte)(ypos / 256);
            command[9] = (byte)(ypos - command[6]*256);

            int sum = 0;
            for (int i = 2; i < command.Length - 1; i++)
            {
                sum += command[i];
            }
            command[10] = (byte)(sum % 256);
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("testimage/id/{id}")]
        public DataResponse<bool> SendTestImageCommand([FromRoute] long id, [FromQuery] byte value)
        {

            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendTestImageCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8 };
            command[6] = value;
            int sum = 0;
            for (int i = 2; i < command.Length - 1; i++)
            {
                sum += command[i];
            }
            command[10] = (byte)(sum % 256);
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("luminance/id/{id}")]
        public DataResponse<bool> SendLuminanceCommand([FromRoute] long id, [FromQuery] byte value)
        {

            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendLuminanceCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x57, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8 };
            command[6] = value;
            int sum = 0;
            for (int i = 2; i < command.Length - 1; i++)
            { 
                sum += command[i];
            }
            command[10] = (byte)(sum % 256);
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }

        [HttpGet("contrast/id/{id}")]
        public DataResponse<bool> SendContrastCommand([FromRoute] long id, [FromQuery] byte value)
        {

            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<bool>();
            }
            bool sendResult;
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(targetIp), targetPort), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient("SendContrastCommand", null);
            byte[] command = new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0x58, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8 };
            command[6] = value;
            int sum = 0;
            for (int i = 2; i < command.Length - 1; i++)
            {
                sum += command[i];
            }
            command[10] = (byte)(sum % 256);
            sendResult = client.Send(command);
            return HandleData(sendResult);
        }


        [HttpGet("queryworkHours/id/{id}")]
        public DataResponse<CommandResponse> QueryWorkHoursCommand([FromRoute] long id)
        {
            string targetIp;
            int targetPort;
            if (!getIPPort(id, out targetIp, out targetPort))
            {
                return InvalidParameter<CommandResponse>();
            }
            this.asd(targetIp, targetPort, new byte[] { 0x55, 0xAA, 0x05, 0x07, 0x05, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xB1, 0xF8 }, out var res);
            Console.Write(BitConverter.ToString(res));
            return HandleData(new CommandResponse()
            {
                value = res[6] * 256 + res[7],
                status = true
            });
        }

        private void asd(string ip, int port, byte[] data, out byte[] res)
        {
            res = Array.Empty<byte>();
            byte[] zxc = Array.Empty<byte>();
            using var server = new MyUDPServer(new IPEndPoint(IPAddress.Parse(ip), port), Logger, $"{Guid.NewGuid():N}");
            using var client = server.GetClient(Guid.NewGuid().ToString("N"), null);
            using var ae = new AutoResetEvent(false);
            client.GotData += ((data) =>
            {
                zxc = data;
                 _ = ae.Set();
            });
            _ = client.Send(data);
            _ = ae.WaitOne();
            res = zxc;
        }
        private async Task<List<DevParam>> GetAllDevParams()
        {
            CacheItemSettings cacheSett = new CacheItemSettings();
            cacheSett.SaveToAll = true;
            return await GCCContext.GetOrSet<List<DevParam>>("DevParams", () =>
            {
                return Task.Run(() =>
                {
                    var dbres = AbsEntrance.DBManager!.LocalDB!.Select<DevParam>().ToList();
                    return dbres;
                });
            }, cacheSett);
        }

        private bool getIPPort(in long id, out string ip, out int port)
        {
            bool sendResult;
            CacheItemSettings cacheSett = new CacheItemSettings();
            cacheSett.SaveToAll = true;
            // var paraList = DeviceCache.Instance.GetAllDevParams().Result;
            var paraList = GetAllDevParams().Result;
            if (paraList is null)
            {
                ip = string.Empty;
                port = 0;
                return false;
            }
            string targetIp = "";
            int targetPort = 0;
            foreach (var item in paraList)
            {
                if (item.DevID == id && item.ParamName == "targetIP")
                {
                    targetIp = item.Value;
                }
                if (item.DevID == id && item.ParamName == "Port")
                {
                    targetPort = Int32.Parse(item.Value);
                }
            }

            Console.WriteLine(DateTime.Now.ToString() + "lsltest, {0}, {1}, {2}", paraList.Count, targetIp, targetPort);

            if (string.IsNullOrWhiteSpace(targetIp) || targetPort == 0)
            {
                ip = string.Empty;
                port = 0;
                return false;
            }

            ip = targetIp;
            port = targetPort;
            return true;
        }




    }
}

/*
 
 [HttpGet("commands/workHours")]
        public DataResponse<CommandResponse> queryWorkHoursCommand()
        {
            int result = -1;
            bool sendResult = false;

            // _ = Task.Run(() =>
            //{
            Console.WriteLine("queryWorkHoursCommand client size:{0}", _allClicnet.Count);

             
           
                using var pack = new Package( DeviceCommManager.Instance.Server.GetClient(guid...., clientfocusQueryIP));
                pack.Send("55 AA 05 07 05 A0 00 00 00 00 B1 F8");
                pack.Wait();
                string[] sArray = pack.Result.Split(" ");
                if (sArray.Length != 12)
                {
                    result = -1;
                }
                if (isValid(sArray))
                {
                    result = (Convert.ToInt32(sArray[6], 16) * 256) + Convert.ToInt32(sArray[7], 16);
                } 

            // });
            var res = new CommandResponse();
            res.value = result;
            res.status = sendResult;
            return HandleData(res);
        }

        private class Package : IDisposable
        {
            private readonly AbsCommUDPClient<string> _client;
            private readonly AutoResetEvent _lock = new AutoResetEvent(false);
            public string Result { get; private set; } = "";
            public Package(AbsCommUDPClient<string> client)
            {
                this._client = client;
            }
            public void Send(string data)
            {
                _ = _client.Send(data);
                _client.GotData += Release;
            }
            private void Release(string data)
            {
                Result = data;
                this._client.GotData -= Release;
                _ = this._lock.Set();
            }
            public void Wait()
            {
                _ = this._lock.WaitOne();
            }


            public void Dispose()
            {
                this._lock.Dispose();
            }
        } 

        private static bool isValid(string[] resultList)
        {
            if (Convert.ToInt32(resultList[0]) != head1 ||
                Convert.ToInt32(resultList[1]) != head2
                || Convert.ToInt32(resultList[resultList.Length - 1]) != tail)
            {
                return false;
            }
            int sum = 0;
            for (int i = 2; i < resultList.Length - 2; i++)
            {
                sum += Convert.ToInt32(resultList[i], 16);
            }
            int modValue = sum % 256;
            return modValue == Convert.ToInt32(resultList[resultList.Length - 2], 16);
        }
 */